package com.wlyuan.open.infrastructure.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wlyuan.mybatis.utils.RepositoryUtils;
import com.wlyuan.open.dao.entity.OpenApiDocDO;
import com.wlyuan.open.dao.mapper.OpenApiDocMapper;
import com.wlyuan.open.domain.api.OpenApiDoc;
import com.wlyuan.open.domain.api.respository.OpenApiDocRepository;
import com.wlyuan.open.domain.api.valueobj.OpenApiDocId;
import com.wlyuan.open.infrastructure.converter.OpenApiDocConverter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotNull;

/**
 * @author yuanzheng
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class OpenApiDocRepositoryImpl implements OpenApiDocRepository {
    private final OpenApiDocMapper openApiDocMapper;


    @Override
    public OpenApiDoc ofId(@NotNull OpenApiDocId openApiDocId) {
        return OpenApiDocConverter.convert(openApiDocMapper.selectById(openApiDocId.getId()));
    }

    @Override
    public OpenApiDocId save(OpenApiDoc openApiDoc) {
        var apiDocDO = RepositoryUtils.save(openApiDocMapper,
                OpenApiDocConverter::convert,
                openApiDoc, openApiDoc.getId());

        openApiDoc.setId(new OpenApiDocId(apiDocDO.getId()));
        return openApiDoc.getId();
    }

    @Override
    public void remove(OpenApiDocId openApiDocId) {

    }

    @Override
    public OpenApiDoc ofApiId(Long apiId) {
        LambdaQueryWrapper<OpenApiDocDO> query = new LambdaQueryWrapper<>();
        query.eq(OpenApiDocDO::getApiId, apiId);
        return OpenApiDocConverter.convert(openApiDocMapper.selectOne(query));
    }
}
